/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.siscoh.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.siscoh.model.bean.AbstractBean;
import org.siscoh.model.bean.EspecificacaoBean;
import org.siscoh.model.bean.EspecificacaoItemBean;

/**
 *
 * @author israel
 */
public class EspecificacaoItemDao extends AbstractDao {

    @Override
    public void update(AbstractBean bean) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void insert(AbstractBean bean) {
        EspecificacaoItemBean espItem = (EspecificacaoItemBean) bean;
        
        
        long id = Conexao.getInstance().insert("especificacao_item",
                new String[]{"item_id"           ,"especificacao_id"           ,"valor"},
                new String[]{espItem.getItem()+"",espItem.getEspecificacao().getId()+"",espItem.getValor()});
        
        if(id == -1)
        {
            System.err.println("Erro ao inserir em especificacao_item!");
        }
        
        espItem.setId(id);
    }
    
    public ArrayList<EspecificacaoItemBean> fetchByItem(long itemId)
    {        
        ResultSet rs = Conexao.getInstance().select(null, "especificacao_item", "item_id = '"+itemId+"'");
        
        return parseRsToObjects(rs);
    }

    @Override
    public void delete(AbstractBean bean) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<?> fetchAllArrayList() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AbstractBean[] fetchAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AbstractBean selectById(long id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected AbstractBean parseRsToObject(ResultSet rs) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected ArrayList<EspecificacaoItemBean> parseRsToObjects(ResultSet rs) {
        ArrayList<EspecificacaoItemBean> list = new ArrayList<EspecificacaoItemBean>();
        
        try
        {
            EspecificacaoDao espDao = new EspecificacaoDao();
            while(rs.next())
            {
                EspecificacaoItemBean espItem = new EspecificacaoItemBean();
                
                espItem.setId(rs.getLong("id"));
                espItem.setItem(rs.getLong("item_id"));
                espItem.setValor(rs.getString("valor"));
                
                espItem.setEspecificacao( (EspecificacaoBean) espDao.selectById(rs.getLong("especificacao_id")));
                
                list.add(espItem);                
            }
        } catch (SQLException ex) {
            ex.printStackTrace(System.err);
        }
        finally{
            try {
                rs.close();
            } catch (SQLException ex) {
                ex.printStackTrace(System.err);
            }
        }
        
        return (list.isEmpty() ? null : list);
    }
    
}
